﻿
'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'
'
'
'*******************************************************************


Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging



Imports System
Imports System.ComponentModel
Imports System.Collections.Generic

Imports System.Collections.ObjectModel


Imports System.Data
Imports System.IO
Imports System.Xml
Imports System.Collections

Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Visio



Imports DC_Rack.Support.clsVisioSupport
Imports DC_Rack.Objects.clsDC_RackData



Public Class clsClients


#Region " Properties and definitions "

    Public m_db As Database
    Public m_ds As DataSet
    Public m_dt As DataTable

    Public m_dc As dc_Client
    Public m_oc As Collection(Of dc_Client)

    Private strUserId As String

    Public m_deleteCommand As DbCommand
    Public m_insertCommand As DbCommand
    Public m_updateCommand As DbCommand


    Public m_clsVisio As DC_Rack.Support.clsVisioSupport

    Public Const readall As String = "selecttblClient"

    ' who am I
    Private myEnum As DC_RackTables = DC_RackTables.tblClient


#End Region

#Region " Move/Transform Data "

    '
    ' create columns based on the initialized/current client object
    '
    Public Sub CreateTableColumns(ByRef tempTable As DataTable)

        Dim newDC As dc_Client = New dc_Client(strUserId)

        Try

            Dim colclientKey As New System.Data.DataColumn
            colclientKey.ColumnName = "clientKey"
            colclientKey.DataType = GetType(Integer)
            colclientKey.DefaultValue = m_dc.clientKey
            tempTable.Columns.Add(colclientKey)

            Dim colclientId As New System.Data.DataColumn
            colclientId.ColumnName = "clientId"
            colclientId.DataType = GetType(String)
            colclientId.DefaultValue = m_dc.clientID
            tempTable.Columns.Add(colclientId)

            Dim colclientName As New System.Data.DataColumn
            colclientName.ColumnName = "clientName"
            colclientName.DataType = GetType(String)
            colclientName.DefaultValue = m_dc.clientName
            tempTable.Columns.Add(colclientName)

            Dim colclientContactName As New System.Data.DataColumn
            colclientContactName.ColumnName = "clientContactName"
            colclientContactName.DataType = GetType(String)
            colclientContactName.DefaultValue = m_dc.clientContactName
            tempTable.Columns.Add(colclientContactName)

            Dim colclientContactTitle As New System.Data.DataColumn
            colclientContactTitle.ColumnName = "clientContactTitle"
            colclientContactTitle.DataType = GetType(String)
            colclientContactTitle.DefaultValue = m_dc.clientContactTitle
            tempTable.Columns.Add(colclientContactTitle)

            Dim colclientAddress As New System.Data.DataColumn
            colclientAddress.ColumnName = "clientAddress"
            colclientAddress.DataType = GetType(String)
            colclientAddress.DefaultValue = m_dc.clientAddress
            tempTable.Columns.Add(colclientAddress)

            Dim colclientCity As New System.Data.DataColumn
            colclientCity.ColumnName = "clientCity"
            colclientCity.DataType = GetType(String)
            colclientCity.DefaultValue = m_dc.clientCity
            tempTable.Columns.Add(colclientCity)

            Dim colclientState As New System.Data.DataColumn
            colclientState.ColumnName = "clientState"
            colclientState.DataType = GetType(String)
            colclientState.DefaultValue = m_dc.clientState
            tempTable.Columns.Add(colclientState)

            Dim colclientZip As New System.Data.DataColumn
            colclientZip.ColumnName = "clientZip"
            colclientZip.DataType = GetType(String)
            colclientZip.DefaultValue = m_dc.clientZip
            tempTable.Columns.Add(colclientZip)

            Dim colclientZipP4 As New System.Data.DataColumn
            colclientZipP4.ColumnName = "clientZipP4"
            colclientZipP4.DataType = GetType(String)
            colclientZipP4.DefaultValue = m_dc.clientZipP4
            tempTable.Columns.Add(colclientZipP4)


            Dim colclientCountry As New System.Data.DataColumn
            colclientCountry.ColumnName = "clientCountry"
            colclientCountry.DataType = GetType(String)
            colclientCountry.DefaultValue = m_dc.clientCountry
            tempTable.Columns.Add(colclientCountry)

            Dim colclientPhone As New System.Data.DataColumn
            colclientPhone.ColumnName = "clientPhone"
            colclientPhone.DataType = GetType(String)
            colclientPhone.DefaultValue = m_dc.clientPhone
            tempTable.Columns.Add(colclientPhone)

            Dim colclientFax As New System.Data.DataColumn
            colclientFax.ColumnName = "clientFax"
            colclientFax.DataType = GetType(String)
            colclientFax.DefaultValue = m_dc.clientFax
            tempTable.Columns.Add(colclientFax)

            Dim colclientComment As New System.Data.DataColumn
            colclientComment.ColumnName = "clientComment"
            colclientComment.DataType = GetType(String)
            colclientComment.DefaultValue = m_dc.clientComment
            tempTable.Columns.Add(colclientComment)

            Dim colclientLastUpdate As New System.Data.DataColumn
            colclientLastUpdate.ColumnName = "clientLastUpdate"
            colclientLastUpdate.DataType = GetType(Date)
            colclientLastUpdate.DefaultValue = m_dc.clientLastUpdate
            tempTable.Columns.Add(colclientLastUpdate)

            Dim colclientLastEdit As New System.Data.DataColumn
            colclientLastEdit.ColumnName = "clientLastEdit"
            colclientLastEdit.DataType = GetType(String)
            colclientLastEdit.DefaultValue = m_dc.clientLastEdit
            tempTable.Columns.Add(colclientLastEdit)

            newDC = Nothing

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If newDC IsNot Nothing Then
            newDC = Nothing
        End If


    End Sub


    '
    ' use for writing records
    '
    Public Sub movePropertiesToDataRow _
            (ByRef tempRow As DataRow, _
             ByVal tempDC As dc_Client)

        Try

            With m_dc
                tempRow.SetField("clientKey", .clientKey)
                tempRow.SetField("clientId", .clientID)
                tempRow.SetField("clientName", .clientName)
                tempRow.SetField("clientContactName", .clientContactName)
                tempRow.SetField("clientContactTitle", .clientContactTitle)
                tempRow.SetField("clientAddress", .clientAddress)
                tempRow.SetField("clientCity", .clientCity)
                tempRow.SetField("clientState", .clientState)
                tempRow.SetField("clientZip", .clientZip)
                tempRow.SetField("clientZipP4", .clientZipP4)
                tempRow.SetField("clientCountry", .clientCountry)
                tempRow.SetField("clientPhone", .clientPhone)
                tempRow.SetField("clientFax", .clientFax)
                tempRow.SetField("clientComment", .clientComment)
                tempRow.SetField("clientLastUpdate", .clientLastUpdate)
                tempRow.SetField("clientLastEdit", strUserId)
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow)

        Try

            With m_dc
                .initProperties()
                .clientKey = tempRow.Item("clientKey")
                .clientID = tempRow.Item("clientId")
                .clientName = tempRow.Item("clientName")
                .clientContactName = tempRow.Item("clientContactName")
                .clientContactTitle = tempRow.Item("clientContactTitle")
                .clientAddress = tempRow.Item("clientAddress")
                .clientCity = tempRow.Item("clientCity")
                .clientState = tempRow.Item("clientState")
                .clientZip = tempRow.Item("clientZip")
                .clientZipP4 = tempRow.Item("clientZipP4")
                .clientCountry = tempRow.Item("clientCountry")
                .clientPhone = tempRow.Item("clientPhone")
                .clientFax = tempRow.Item("clientFax")
                .clientComment = tempRow.Item("clientComment")
                .clientLastUpdate = tempRow.Item("clientLastUpdate")
                .clientLastEdit = tempRow.Item("clientLastEdit")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow, _
             ByRef tempDC As dc_Client)

        Try

            With tempDC
                .initProperties()
                .clientKey = tempRow.Item("clientKey")
                .clientID = tempRow.Item("clientId")
                .clientName = tempRow.Item("clientName")
                .clientContactName = tempRow.Item("clientContactName")
                .clientContactTitle = tempRow.Item("clientContactTitle")
                .clientAddress = tempRow.Item("clientAddress")
                .clientCity = tempRow.Item("clientCity")
                .clientState = tempRow.Item("clientState")
                .clientZip = tempRow.Item("clientZip")
                .clientZipP4 = tempRow.Item("clientZipP4")
                .clientCountry = tempRow.Item("clientCountry")
                .clientPhone = tempRow.Item("clientPhone")
                .clientFax = tempRow.Item("clientFax")
                .clientComment = tempRow.Item("clientComment")
                .clientLastUpdate = tempRow.Item("clientLastUpdate")
                .clientLastEdit = tempRow.Item("clientLastEdit")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub moveDataTableToObservableCollection _
            (ByVal dtTemp As DataTable)

        Try

            If 0 < dtTemp.Rows.Count Then

                If m_oc IsNot Nothing Then
                    m_oc = Nothing
                    m_oc = New Collection(Of dc_Client)
                Else
                    m_oc = New Collection(Of dc_Client)
                End If

                Dim drTemp As DataRow
                Dim dcTemp As dc_Client
                For Each drTemp In dtTemp.Rows
                    dcTemp = New dc_Client
                    moveDataRowToProperties(drTemp, dcTemp)
                    m_oc.Add(dcTemp)
                Next

            End If

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Function moveDataTableToXML() As XmlDocument

        Dim writer = New System.IO.StringWriter
        Dim m_dt_xml As Xml.XmlDocument = Nothing

        Try


            m_dt.WriteXml(writer, XmlWriteMode.WriteSchema, True)
            m_dt_xml = Nothing
            m_dt_xml = New Xml.XmlDocument
            m_dt_xml.LoadXml(writer.ToString)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally

            writer = Nothing

        End Try

        Return m_dt_xml


    End Function


#End Region

#Region " Standard Calls "


    '
    '
    '
    Public Sub initDataSetAndTable()

        Try

            ' make sure the root stuff is there
            If m_ds Is Nothing Then
                m_ds = New DataSet
            Else
                m_ds = Nothing
                m_ds = New DataSet
            End If
            m_ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema

            If m_dt Is Nothing Then
                m_dt = New DataTable
            Else
                m_dt = Nothing
                m_dt = New DataTable
            End If

            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_Client)
            Else
                m_oc = Nothing
                m_oc = New Collection(Of dc_Client)
            End If

            If m_dc Is Nothing Then
                m_dc = New dc_Client(strUserId)
            Else
                m_dc = Nothing
                m_dc = New dc_Client(strUserId)
            End If


            CreateTableColumns(m_dt)
            m_dt.TableName = dc_racktables.tblClient.tostring
            m_ds.Tables.Add(m_dt)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub

    '
    ' take the existing Table and put it back into the database
    ' with any changes that may have occured
    '
    Public Function dbUpdateDataSet() As Int16

        Dim rowsAffected As Int16 = 0

        Try

            rowsAffected = m_db.UpdateDataSet(m_ds, dc_racktables.tblClient.tostring, _
                                        m_insertCommand, _
                                        m_updateCommand, _
                                        m_deleteCommand, _
                                        UpdateBehavior.Standard)
            'msgbox rowsAffected

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If

        End Try

        Return rowsAffected

    End Function

    '
    '
    '
    Private Sub buildInsertClientCommand()



        Try
            m_insertCommand = m_db.GetStoredProcCommand("insertClient")

            Dim cmdActive As DbCommand
            cmdActive = m_insertCommand

            m_db.AddInParameter(cmdActive, "clientId", DbType.String, "clientId", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientName", DbType.String, "clientName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientContactName", DbType.String, "clientContactName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientContactTitle", DbType.String, "clientContactTitle", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientAddress", DbType.String, "clientAddress", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientCity", DbType.String, "clientCity", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientState", DbType.String, "clientState", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientZip", DbType.String, "clientZip", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientZipP4", DbType.String, "clientZipP4", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientCountry", DbType.String, "clientCountry", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientPhone", DbType.String, "clientPhone", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientFax", DbType.String, "clientFax", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientComment", DbType.String, "clientComment", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientLastUpdate", DbType.Date, "clientLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientLastEdit", DbType.String, "clientEdit", DataRowVersion.Current)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildUpdateClientCommand()



        Try
            m_updateCommand = m_db.GetStoredProcCommand("updateClient")

            Dim cmdActive As DbCommand
            cmdActive = m_updateCommand

            m_db.AddInParameter(cmdActive, "clientKey", DbType.Int32, "clientKey", DataRowVersion.Current)

            m_db.AddInParameter(cmdActive, "clientId", DbType.String, "clientId", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientName", DbType.String, "clientName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientContactName", DbType.String, "clientContactName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientContactTitle", DbType.String, "clientContactTitle", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientAddress", DbType.String, "clientAddress", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientCity", DbType.String, "clientCity", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientState", DbType.String, "clientState", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientZip", DbType.String, "clientZip", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientZipP4", DbType.String, "clientZipP4", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientCountry", DbType.String, "clientCountry", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientPhone", DbType.String, "clientPhone", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientFax", DbType.String, "clientFax", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientComment", DbType.String, "clientComment", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientLastUpdate", DbType.Date, "clientLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "clientLastEdit", DbType.String, "clientEdit", DataRowVersion.Current)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildDeleteClientCommand()

        Try
            m_deleteCommand = m_db.GetStoredProcCommand("deleteClient")

            Dim cmdActive As DbCommand
            cmdActive = m_deleteCommand

            m_db.AddInParameter(cmdActive, "clientKey", DbType.Int32, "clientKey", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub

    '
    ' create a record based on property values move it to the generic datatable
    ' then copy the table to the database
    '
    Public Function addClientsDataSourceRecord _
        (ByVal tempDC As dc_Client, _
         Optional ByVal blnSilent As Boolean = False) _
        As Integer

        Dim intRowsAffected As Integer = 0

        Try

            initDataSetAndTable()

            Dim m_dr As DataRow = m_ds.Tables(dc_racktables.tblClient.tostring).NewRow
            movePropertiesToDataRow(m_dr, tempDC)
            m_ds.Tables(dc_racktables.tblClient.tostring).Rows.Add(m_dr)
            intRowsAffected = dbUpdateDataSet()


            If blnSilent = False Then
                MsgBox("DataRowsUpated = " & intRowsAffected)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return intRowsAffected

    End Function





#End Region

#Region " database calls "


    '
    '
    '
    Public Sub dbSelectAllClients()


        Try


            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand(readall)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblClient.tostring)

            m_dt = m_ds.Tables(dc_racktables.tblClient.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub dbSelectClientByClientKey(ByVal lngKey As Int32)

        Try

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectClientByClientKey")
            m_db.AddInParameter(cmd, "@clientKey", DbType.Int32, lngKey)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblClient.tostring)
            m_dt = m_ds.Tables(dc_racktables.tblClient.tostring)

            ' if we find the record move it to the properties
            If m_dt.Rows.Count = 1 Then
                Dim m_dr As DataRow = m_dt.Rows(0)
                moveDataRowToProperties(m_dr)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub



#End Region

#Region " constructor "

    Public Sub New()
        Try


            m_db = DatabaseFactory.CreateDatabase()

            strUserId = Environment.UserDomainName & "/" & Environment.UserName

            '
            ' init an object class so that the default constants are available
            ' to the table definitions
            '
            If m_dc Is Nothing Then
                m_dc = New dc_Client(strUserId)
            Else
                m_dc = Nothing
                m_dc = New dc_Client(strUserId)
            End If

            initDataSetAndTable()


            m_clsVisio = New DC_Rack.Support.clsVisioSupport

            m_updateCommand = Nothing
            m_insertCommand = Nothing
            m_deleteCommand = Nothing

            buildInsertClientCommand()
            buildDeleteClientCommand()
            buildUpdateClientCommand()


        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


#End Region




End Class
